package top.fangw.oauth.authentication.oicd;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import top.fangw.oauth.model.SysUserEntity;

import java.util.Map;

/**
 * Example service to perform lookup of user info for customizing an {@code id_token}.
 */
@Service
public class MyOidcUserInfoService {


    public MyOidcUserInfo loadUser(String username) {
        PasswordEncoder encoder = new BCryptPasswordEncoder();
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setId(Integer.valueOf("123456"));
        sysUserEntity.setUsername("admin");
        sysUserEntity.setName("admin");
        sysUserEntity.setPassword(encoder.encode("123456"));
//        SysUserEntity sysUserEntity = sysUserService.selectByUsername(username);
        return new MyOidcUserInfo(this.createUser(sysUserEntity));
    }

    private Map<String, Object> createUser(SysUserEntity sysUserEntity) {
        return MyOidcUserInfo.myBuilder()
                .name(sysUserEntity.getName())
                .username(sysUserEntity.getUsername())
                .description(sysUserEntity.getDescription())
                .status(sysUserEntity.getStatus())
                .phoneNumber(sysUserEntity.getUsername())
                .email(sysUserEntity.getUsername() + "@example.com")
                .profile("https://example.com/" + sysUserEntity.getName())
                .address("XXX共和国XX省XX市XX区XXX街XXX号")
                .build()
                .getClaims();
    }

}

